"use strict";

$(function(){
	let url = localStorage.getItem("apiUrl");
	let name = localStorage.getItem("shopName");
	if( url && name ){
		$("#shopSpan").text(name);
		$(".connent").show();
		$(".config").hide();
	}
	
	$("#yes").on("click",function(){
		let api = $("#url").val();
		let shop = $("#shop").val();
		shop = shop.trim().toUpperCase();
		if(api==''||shop==''){
			alert("地址店铺不能为空");
			return;
		}
		localStorage.setItem("apiUrl",api);
		localStorage.setItem("shopName",shop);
		$("#shopSpan").text(shop);
		$(".connent").show();
		$(".config").hide();
	});

	$("#login").on("click",function(){
		let acc = $("#acc").val();
		let pwd = $("#pwd").val();
		
		if(acc==''||pwd==''){
			alert("帐号密码不能为空");
			return;
		}

		let shopName = localStorage.getItem("shopName");
		let arr = acc.split("@");
		if(! (arr[1] && arr[1].trim().toUpperCase() === shopName) ){
			alert("登录店铺错误");
			return;
		}

		let msg ={
			acc:arr[0],
			pwd:pwd,
			shop:shopName,
			type:"amazon-login"
		}
		chrome.runtime.sendMessage(msg);
	});

	document.onkeydown = function(e){
		let ev = document.all ? window.event : e;
		if(ev.keyCode==13) {
			$('#login').click();
		}
	}
})